package com.spring.demo.init.rmi;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.remoting.rmi.RmiServiceExporter;

import com.spring.demo.core.rmi.IRMIUserService;
import com.spring.demo.core.rmi.RMIUserService;

/**
 * rmi server config
 * 
 * @author sean
 * 
 */
@Configuration
@PropertySource({ "classpath:rmi/rmi-server.properties" })
public class RmiServerConfig {

	/**
	 * rmi 服务名
	 */
	public static final String RMI_SERVER_NAME_PROPERTY_NAME = "serverName";
	/**
	 * rmi 注册端口
	 */
	public static final String RMI_REGISTRY_PORT_PROPERTY_NAME = "registryPort";

	@Autowired
	private Environment envir;

	@Bean
	public IRMIUserService rmiUserService() {
		return new RMIUserService();
	}

	@Bean
	@Autowired
	public RmiServiceExporter rmiServiceExporter(IRMIUserService rmiUserService) {
		RmiServiceExporter rmiServiceExporter = new RmiServiceExporter();

		rmiServiceExporter.setServiceName(envir
				.getProperty(RMI_SERVER_NAME_PROPERTY_NAME));
		rmiServiceExporter
				.setServicePort(envir.getRequiredProperty(
						RmiServerConfig.RMI_REGISTRY_PORT_PROPERTY_NAME,
						Integer.class));
		rmiServiceExporter.setService(rmiUserService);
		rmiServiceExporter.setServiceInterface(IRMIUserService.class);

		return rmiServiceExporter;
	}
}
